using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Configuration;
using System.Web;
using System.Web.Security;

/// <summary>
/// Snap! Diagnostics
/// -----------------
/// Adds all of the necessary tables and procedures
/// for logging diagnostics error messages and recalling
/// those errors
/// -----------------
/// By Aaron Stannard 7/28/2007
/// </summary>
namespace Snap.Diagnostics
{
    public class Diagnostics
    {
        private String connectionString;

        public Diagnostics(String connectionStringName)
        {
            //Get the default connection string
            this.connectionString = WebConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
        }

        /// <summary>
        /// Installs all of the Diagnostics functionality needed for
        /// logged errors using Snap! Diagnostics
        /// </summary>
        /// <returns>true if installation was successful, false if otherwise</returns>
        public bool InstallDiagnostics()
        {
            return true;
        }

        public bool logError(SError error)
        {
            bool returnValue = true;
            SqlConnection con = new SqlConnection(this.connectionString);
            SqlCommand cmd = new SqlCommand("SnapDiagnostics_AddError", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@Page", SqlDbType.NVarChar, 256));
            cmd.Parameters["@Page"].Value = error.page;
            cmd.Parameters.Add(new SqlParameter("@Method", SqlDbType.NVarChar, 256));
            cmd.Parameters["@Method"].Value = error.method;
            cmd.Parameters.Add(new SqlParameter("@Params", SqlDbType.NVarChar, 256));
            cmd.Parameters["@Params"].Value = error.parameters;
            cmd.Parameters.Add(new SqlParameter("@Time", SqlDbType.DateTime));
            cmd.Parameters["@Time"].Value = error.time;
            cmd.Parameters.Add(new SqlParameter("@Line", SqlDbType.SmallInt));
            cmd.Parameters["@Line"].Value = error.line;
            cmd.Parameters.Add(new SqlParameter("@Message", SqlDbType.NVarChar, 256));
            cmd.Parameters["@Message"].Value = error.msg;
            try
            {
                con.Open(); //Connect to the database
                cmd.ExecuteNonQuery(); //We are not going to receive a result set
            }
            catch (SqlException err)
            {
                returnValue = false;
                throw new ApplicationException(err.Message + err.LineNumber);
            }
            finally
            {
                con.Close(); //Clean up
            }
            return returnValue;
        }
    }

    public class SError
    {
        private String msg_;
        public String msg
        {
            get
            {
                return this.msg_;
            }
            set
            {
                this.msg_ = value;
            }
        }

        private String page_;
        public String page
        {
            get
            {
                return this.page_;
            }
            set
            {
                this.page_ = value;
            }
        }

        private String method_;
        public String method
        {
            get
            {
                return this.method_;
            }
            set
            {
                this.method_ = value;
            }

        }

        private String parameters_;
        public String parameters{
            get
            {
                return this.parameters_;
            }
            set
            {
                this.parameters_ = value;
            }
        }

        private short line_;
        public short line
        {
            get
            {
                return this.line_;
            }
            set
            {
                this.line_ = value;
            }
        }

        private DateTime time_;
        public DateTime time
        {
            get
            {
                return this.time_;
            }
            set
            {
                this.time_ = value;
            }
        }

        public SError(String page, String method, String parameters,
            short line, String msg)
        {
            this.page = page;
            this.method = method;
            this.parameters = parameters;
            this.time = DateTime.Now; //Get the datetime data for this moment
            this.line = line;
            this.msg = msg;
        }
    }
}